// pages/course/index.js
const app = getApp()
const $api = require("../../api/course").API
const $banner = require("../../api/banner").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList:[],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    typeList:[],
    courseList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    t.getKeyValueByKeyType()
    t.getBannerByOwnerShip()
    t.getCourseList()
    // t.getSuitCourseList()
  },

  // 跳转课程分类
  gototype(e){
    var t = this
    wx.navigateTo({
      url:'/course/pages/type/index?type='+e.currentTarget.dataset.type
    })
  },
  gotoinfo(e){
    var t = this
    wx.navigateTo({
      url:'/course/pages/info/index?id='+e.currentTarget.dataset.id
    })
  },
  // 获取排序类型
  getKeyValueByKeyType(){
    var t = this,
    data = {
      dict_type:"CourseType"
    },
    typeList = wx.getStorageSync('coursetypelist')
    if(typeList){
      t.setData({
        typeList:typeList
      })
      return false
    }
   $api.getKeyValueByKeyType(data).then(res=>{
     wx.setStorageSync('coursetypelist', res.data)
     t.setData({
      typeList:res.data
     })
   })
  },
   // 获取推荐列表
   getCourseList(){
    var t = this,
    data={
      pageNo:1,
    }
    $api.getRecommendCourseList(data).then(res=>{
      console.log(res)
      t.setData({
        courseList:res.data
      })
    })
   },
   // 获取适合课程列表
  getSuitCourseList(){
    var t = this,
    data = {
      member_id:wx.getStorageSync('memberinfo').member_id
    }
    $api.getSuitCourseList(data).then(res=>{
      console.log(res)
    })
  },
  gotocoursesearch(){
    var t = this
    wx.navigateTo({
      url: '/pages/course/search/index',
    })
  },
  // 根据位置获取不同的banner列表
  getBannerByOwnerShip(){
    var t = this,
    data = {
        banner_ownership:2
    }
    $banner.getBannerByOwnerShip(data).then(res=>{
        console.log(res)
        t.setData({
            interval:res.data.banner_refresh,
            bannerList:res.data.detail_list
        })
    })
  },
})